<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search extends MY_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();
		
		//Load model
		$this->load->model('search_model');
				
		//Load Search Script
		$this->data['js'][] = "searchScript.js";
		$this->data['css'][] = "searchStyle.css";
		
		 //Load Library
  		$this->load->library('pagination');		
    }
	
	/*Home*/	 
	public function index()
	{
		//Load Module
		$this->data['module'] = $this->module('search');
		
		$this->data['search_music'] = true;
		$this->data['search_movie'] = true;
		$this->data['search_video'] = true;
		$this->data['search_artist'] = true;
		$this->data['search_album'] = true;
		$this->data['search_news'] = true;
		$this->data['search_chord'] = true;

		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
	}
	
	/*Search*/
	public function get_search($module)
	{
		//if($this->input->post('ajax')) $this->ajax_search_pager($module,TRUE);
		
		//Load Module
		$this->data['module'] = $this->module('search');

		$this->data['dataResult'] = $this->ajax_search_pager($module);
		
		$txtSearch = substr($this->input->get('text'), 0, 190);

		if(!empty($txtSearch)):
			//Set SEO Tag
			$this->data['seo_title'] = 'ค้นหาคำว่า "'.$txtSearch.'" | ค้นหาเพลง ค้นหาภาพยนต์ ค้นหาวิดีโอ ค้นหาศิลปิน ค้นหาอัลบั้ม ค้นหาข่าวบันเทิง ค้นหาคอร์ดเพลง You2Play.com';
			$this->data['seo_description'] = 'ค้นหาคำว่า "'.$txtSearch.'" ระบบค้นหาเพลง ภาพยนต์ วิดีโอ หาปุ๊ปเจอปั๊ป ที่นี่ You2Play.com';
			$this->data['seo_keyword'] = 'ค้นหาเพลง, ค้นหาภาพยนต์, ค้นหาวิดีโอ, ค้นหาศิลปิน, ค้นหาอัลบั้ม, ค้นหาข่าวบันเทิง, ค้นหาคอร์ดเพลง, ฟังเพลง, ฟังเพลงออนไลน์, เพลงไทย, เพลงต่างประเทศ, เพลงเอเชียน';
			//GA Events
			$this->data['ga_event'] = 'CI-Search-'.seo_url($txtSearch);				
		endif;			

		$this->data['content'] = $this->router->class."/search";
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
	}
	
	private function ajax_search_pager($module,$ajax = FALSE)
	{
		//Load Library
		$this->load->library('solr');
						
		$searchModule = '';
		$searchCondition = '';
		$txtSearch = substr($this->input->get('text'), 0, 190);
		$text = $this->solr->escapeSolrValue($txtSearch);
		//$text = preg_replace('/\^/','',$text);
		$this->data['search_music'] = false;
		$this->data['search_movie'] = false;
		$this->data['search_video'] = false;
		$this->data['search_artist'] = false;
		$this->data['search_album'] = false;
		$this->data['search_news'] = false;
		$this->data['search_chord'] = false;
		$module_arr = explode(',',$module);
		 //Music   Movie   Video   Artist   Album   News Chord
		foreach($module_arr as $value):
			switch($value):
				case 'music':
					$searchModule .= 'id:song_* OR ';
					$searchCondition .= 'song_name_s:"'.$text.'" OR song_tag_s:"'.$text.'" OR ';
					$this->data['search_music'] = true;
				break;
				case 'movie':
					$searchModule .= 'id:movie_* OR ';
					$searchCondition .= 'movie_name_s:"'.$text.'" OR movie_tag_s:"'.$text.'" OR ';
					$this->data['search_movie'] = true;				
				break;
				case 'video':
					$searchModule .= 'id:video_* OR ';
					$searchCondition .= 'video_name_s:"'.$text.'" OR video_tag_s:"'.$text.'" OR ';
					$this->data['search_video'] = true;					
				break;
				case 'artist':
					$searchModule .= 'id:artist_* OR ';
					$searchCondition .= 'artist_name_s:"'.$text.'" OR artist_tag_s:"'.$text.'" OR ';
					$this->data['search_artist'] = true;			
				break;
				case 'album':
					$searchModule .= 'id:album_* OR ';
					$searchCondition .= 'album_name_s:"'.$text.'" OR album_tag_s:"'.$text.'" OR ';
					$this->data['search_album'] = true;		
				break;
				case 'news':
					$searchModule .= 'id:news_* OR ';
					$searchCondition .= 'news_name_s:"'.$text.'" OR news_tag_s:"'.$text.'" OR ';
					$this->data['search_news'] = true;		
				break;
				case 'chord':
					$searchModule .= 'id:chords_* OR ';
					$searchCondition .= 'chords_name_s:"'.$text.'" OR chords_tag_s:"'.$text.'" OR ';
					$this->data['search_chord'] = true;		
				break;				
				default:
					$this->page_missing();
			endswitch;
		endforeach;
		
		if(!empty($searchModule) && !empty($searchCondition)):
			$searchModule = '('.rtrim($searchModule,' OR ').')';
			$searchCondition = '('.rtrim($searchCondition,' OR ').')';		
		else:
			$searchModule = '(id:song_* OR id:movie_* OR id:video_* OR id:artist_* OR id:album_* OR id:news_* OR id:chords_*)';
			$searchCondition = '(song_name_s:"'.$text.'" OR song_tag_s:"'.$text.'" OR movie_name_s:"'.$text.'" OR movie_tag_s:"'.$text.'" OR video_name_s:"'.$text.'" OR video_tag_s:"'.$text.'" OR artist_name_s:"'.$text.'" OR artist_tag_s:"'.$text.'" OR album_name_s:"'.$text.'" OR album_tag_s:"'.$text.'" OR news_name_s:"'.$text.'" OR news_tag_s:"'.$text.'" OR chords_name_s:"'.$text.'" OR chords_tag_s:"'.$text.'")';
			$this->data['search_music'] = true;	
			$this->data['search_movie'] = true;	
			$this->data['search_video'] = true;	
			$this->data['search_artist'] = true;
			$this->data['search_album'] = true;	
			$this->data['search_news'] = true;
			$this->data['search_chord'] = true;
		endif;

		$query = $searchModule.' AND '.$searchCondition;
		/*Pager*/		
		$config['base_url'] = current_url().'?'.query_string(array('p'));
		$config['total_rows'] = $this->solr->query($query)->count_all_results();
		$config['per_page'] = 15;
		$config['num_links'] = 5;
		$config['first_link'] = '&laquo;';
		$config['first_tag_open'] = '<span class="first">';
		$config['first_tag_close'] = '</span>';				
		$config['last_link'] = '&raquo;';
		$config['last_tag_open'] = '<span class="last">';
		$config['last_tag_close'] = '</span>';		
		$config['next_tag_open'] = '<span class="prev">';
		$config['next_tag_close'] = '</span>';	
		$config['prev_tag_open'] = '<span class="next">';
		$config['prev_tag_close'] = '</span>';			
		$config['cur_tag_open'] = '<span class="num_tag current">';
		$config['cur_tag_close'] = '</span>';
		$config['num_tag_open'] = '<span class="num_tag">';
		$config['num_tag_close'] = '</span>';				
		$config['page_query_string'] = TRUE;
		$config['query_string_segment'] = 'p';
		$this->pagination->initialize($config);
		$this->data['pager'] = $this->pagination->create_links();
		$this->data['dataSearch'] = $this->solr->query($query)->limit(intval($this->input->get('p')),$config['per_page'])->result();
		foreach($this->data['dataSearch'] as $value):
			if(preg_match('/album_/',$value->id)):
				$id = str_replace('album_','',$value->id);
				$value->artist = $this->db->select('j.artist_id,k.artist_name')
				->join('song_artist as j','j.song_id=t.song_id','INNER')
				->join('artist_info as k','k.artist_id=j.artist_id','INNER')
				->get_where('song_album as t',array('t.album_id' => $id,'t.is_default' => '1','j.is_default' => '1'),1)
				->row();
				if(empty($value->artist)): //if no artist relate then delete form solr
					$this->solr->deleteById($value->id);
					$this->solr->commit(); //commit to see the deletes and the document
					$this->solr->optimize(); //merges multiple segments into one				
				endif;
			endif;
		endforeach;
		//if result id valid then record keyword tag
		if(!empty($this->data['dataSearch'])):
			$this->search_model->keyword_tag_update(trim($this->input->get('text')));
		endif;
		
		if($ajax):
			echo $this->load->view($this->router->class."/ajax_search_pager", $this->data, TRUE);
			exit;
		else:
			return $this->load->view($this->router->class."/ajax_search_pager", $this->data, TRUE);		
		endif;		
	}
}


/* End of file video.php */
/* Location: ./application/controllers/video.php */